<?php
    class InitCache extends APIBase {
		
		public $tag = "InitCache";
		public $isLogin = false;
		
		//define(TIME_STAMP, time());
		public function before() {
			$this->initMysql();
			$this->initCacheRedis();
			$this->initDataRedis(0);
			@define(TIME_STAMP, time());
			return true;
		}

		public function logic() {	
		   		
			if (!(isset($this->param['pass']) && $this->param['pass'] == Config::$cache_pass)) {
				$this->returnError(300, "password is error.");
			}
			//4832
			//初始化场馆、道具、奖品列表
			//$this->cache_redis->hMget("hv:4832");
			//$this->cache_redis->hMset('vga', array('upvenus'=>'0','upgoods'=>'0'));
			//初始化大厅总人数 
			$this->cache_redis->hSet('playing','total',rand(100,200));
			
			
			if (isset($this->param['all']) && $this->param['all'] == 1) {
				$flag = true;
			}else{
			    $flag = false;	
			}
			/*
			
			
			if ($flag || (isset($this->param['announcement']) && $this->param['announcement'] == 1)) {
				$this->LoadAnnouncement();
			}
			
			
			
			if ($flag || (isset($this->param['marquee']) && $this->param['marquee'] == 1)) {
				$this->LoadMarquee();
			}
			
			if ($flag || (isset($this->param['gift']) && $this->param['gift'] == 1)) {
				$this->LoadGift();
			}

			if ($flag || (isset($this->param['award']) && $this->param['award'] == 1)) {
				$this->LoadAward();
			}
			
			if ($flag || (isset($this->param['cointasklist']) && $this->param['cointasklist'] == 1)) {
				$this->LoadCoinTaskList();
			}
			
		    //游戏配置
			if ($flag || (isset($this->param['gameconfig']) && $this->param['gameconfig'] == 1)) {
				$this->gameconfig();
			}
			//元宝兑换
			if ($flag || (isset($this->param['changecoinmoney']) && $this->param['changecoinmoney'] == 1)) {
				$this->LoadChangeCoinMoney();
			}
			
			
			
			//奖励池列表
            if ($flag || (isset($this->param['rewardpool']) && $this->param['rewardpool'] == 1)) {
				$this->LoadRewardpool();
			}
			//分享奖励列表
			
            if ($flag || (isset($this->param['sharereward']) && $this->param['sharereward'] == 1)) {
				$this->LoadShareReward();
			}
			//签到奖励列表
			 if ($flag || (isset($this->param['signreward']) && $this->param['signreward'] == 1)) {
				$this->LoadSignReward();
			}
			//排行榜奖励列表
			if ($flag || (isset($this->param['boardreward']) && $this->param['boardreward'] == 1)) {
				$this->LoadBoardReward();
			}
			//救济金奖励列表
			if ($flag || (isset($this->param['almsreward']) && $this->param['almsreward'] == 1)) {
				$this->LoadAlmsReward();
			}
			//vip配置列表
			if ($flag || (isset($this->param['vipconfig']) && $this->param['vipconfig'] == 1)) {
				$this->LoadVipConfig();
			}
			*/
			
			//商品列表
			if ($flag || (isset($this->param['goods']) && $this->param['goods'] == 1)) {
				$this->LoadGoods();
			}
			//道具定义表
			if ($flag || (isset($this->param['getprops']) && $this->param['getprops'] == 1)) {
				$this->LoadGetProps();
			}
			//支付方式配置列表
			if ($flag || (isset($this->param['payment']) && $this->param['payment'] == 1)) {
				$this->LoadPayment();
			}
			//邮件配置列表
			if ($flag || (isset($this->param['email']) && $this->param['email'] == 1)) {
				$this->LoadEmail();
			}
			//奖励列表
			if ($flag || (isset($this->param['getrewards']) && $this->param['getrewards'] == 1)) {
				$this->LoadGetReward();
			}
			//游戏列表
			if ($flag || (isset($this->param['games']) && $this->param['games'] == 1)) {
				$this->LoadGetGames();
			}
			//房间信息
			if ($flag || (isset($this->param['venue']) && $this->param['venue'] == 1)) {
				$this->loadVenue();
			}
			//推送服配置
			if ($flag || (isset($this->param['push']) && $this->param['push'] == 1)) {
				$this->loadPush();
			}
			
		}
		
		private  function clear($regex) {
			 
			foreach ($regex as $re) {
				$keys = $this->cache_redis->keys($re);
				foreach ($keys as $key) {
					$this->cache_redis->del($key);
				}
				
			}
		}
		
		private  function clearData($regex) {
			$this->data_redis->select(1);
			foreach ($regex as $re) {
				$keys = $this->data_redis->keys($re);
				foreach ($keys as $key) {
					$this->data_redis->del($key);
				}
				
			}
		}
		
		//邮件信息缓存redis
		private function LoadEmail(){
			$row = $this->mysql->find("SELECT * FROM email_type");
			$row0 = $this->mysql->find("SELECT * FROM email_message");
			$this->clear(array("ht:*"));
			$this->clear(array("hmg:*"));
			$total = 0;
			$total0 = 0;
			foreach($row as $v){
				$id = $v['id'];
				$this->cache_redis->hMset("ht:$id",$v);
				$total++;
			}
            foreach($row0 as $v){
				$id = $v['id'];
				$this->cache_redis->hMset("hmg:$id",$v);
				$total0++;
			}
			$keys = count($this->cache_redis->keys("ht:*"));
			$keys0 = count($this->cache_redis->keys("hmg:*"));
			echo "LoadEmail {ht: {$keys}} hmg: {$keys0} data success...<br>";
			$this->i("LoadEmail {ht: {$keys}} hmg: {$keys0} data success...");
		}
		
		//奖励池信息缓存redis
		private function LoadRewardpool(){
			$row = $this->mysql->find("SELECT id,reward,chance,sort,rwdesc,mid FROM rewardpool");
			$this->clear(array("hrewardpool:*"));
			$total = 0;
			foreach($row as $v){
				$id = $v['id'];
				$this->cache_redis->hMset("hrewardpool:$id", $v);
				$total++;
			}
			$keys = count($this->cache_redis->keys("hrewardpool:*"));
			echo "LoadRewardpool {hrewardpool: {$keys}} data success...<br>";
			$this->i("LoadRewardpool {hrewardpool: {$keys}} data success...");
		}
		
		//分享奖励缓存redis
		private function LoadShareReward(){
			$row = $this->mysql->find("SELECT id,sharenum,sort,reward,shdesc,mid FROM share_reward");
			$this->clear(array("hsharereward:*"));
			$total = 0;
			foreach($row as $v){
				$id = $v['id'];
				$this->cache_redis->hMset("hsharereward:$id", $v);
				$total++;
			}
			$keys = count($this->cache_redis->keys("hsharereward:*"));
			echo "LoadShareReward {hsharereward: {$keys}} data success...<br>";
			$this->i("LoadShareReward {hsharereward: {$keys}} data success...");
		}
		
		//签到奖励缓存redis
		private function LoadSignReward(){
			$row = $this->mysql->find("SELECT id,reward,sort,mid FROM sign_reward");
			$this->clear(array("hsignreward:*"));
			$total = 0;
			foreach($row as $v){
				$id = $v['id'];
				$this->cache_redis->hMset("hsignreward:$id", $v);
				$total++;
			}
			$keys = count($this->cache_redis->keys("hsignreward:*"));
			echo "LoadSignReward {hsignreward: {$keys}} data success...<br>";
			$this->i("LoadSignReward {hsignreward: {$keys}} data success...");
		}
		//排行榜奖励缓存redis
		private function LoadBoardReward(){
			$row = $this->mysql->find("SELECT id,reward,sort,mid FROM board_reward");
			$this->clear(array("hboardreward:*"));
			$total = 0;
			foreach($row as $v){
				$id = $v['id'];
				$this->cache_redis->hMset("hboardreward:$id", $v);
				$total++;
			}
			$keys = count($this->cache_redis->keys("hboardreward:*"));
			echo "LoadBoardReward {hboardreward: {$keys}} data success...<br>";
			$this->i("LoadBoardReward {hboardreward: {$keys}} data success...");
		}
		
		//救济金奖励缓存redis
		private function LoadAlmsReward(){
			$row = $this->mysql->find("SELECT id,rewardone,rewardtwo,rewardthree,chance,mid FROM alms_reward");
			$this->clear(array("halmsreward:*"));
			$total = 0;
			foreach($row as $v){
				$id = $v['id'];
				$this->cache_redis->hMset("halmsreward:$id", $v);
				$total++;
			}
			$keys = count($this->cache_redis->keys("halmsreward:*"));
			echo "LoadAlmsReward {halmsreward: {$keys}} data success...<br>";
			$this->i("LoadAlmsReward {halmsreward: {$keys}} data success...");
		}
		
		//推送服务配置缓存redis
		private function LoadPush(){
			$row = $this->mysql->find("SELECT id,webnet,ip,ports,desces FROM node_ip");
			$this->clearData(array("hnode_ip:*"));
			$this->data_redis->select(1);
			$total = 0;
			foreach($row as $v){
				$id = $v['id'];
				$this->data_redis->hMset("hnode_ip:$id", $v);
				$total++;
			}
			$keys = count($this->data_redis->keys("hnode_ip:*"));
			echo "LoadPush {hnode_ip: {$keys}} data success...<br>";
			$this->i("LoadPush {hnode_ip: {$keys}} data success...");
		}
		
		
		//房间缓存redis
		private function loadVenue() {
			$row = $this->mysql->find("SELECT * FROM `venue` WHERE `status`=1" );
			$this->clear(array("hv:*", "lv:ids"));
			$this->clearData(array("hroom_cfg:*"));
			$this->data_redis->select(1);
			foreach ($row as $v) {
				$id = $v['id'];
				unset($v['id']);
				$list = $this->mysql->find("SELECT * from venue, gameinfo where venue.id = ".$id." and gameinfo.NameID = venue.GameID and gameinfo.`Enable` = 1 and venue.`status` = 1" );
				$this->cache_redis->rPush("lv:ids", $id);
				$this->cache_redis->hMset("hv:{$id}", $v);
				if(!empty($list[0])){
					$this->data_redis->hMset("hroom_cfg:{$id}",$list[0]);
				}
			}
			$hroom_cfg_keys = count($this->data_redis->keys("hroom_cfg:*"));
			$hkeys = count($this->cache_redis->keys("hv:*"));
			$listkeys = count($this->cache_redis->lRange("lv:ids", 0, -1));
			$this->cache_redis->hset("vga", "upvenus",TIME_STAMP);
			echo "LoadVenue {lv: {$hkeys}, hv: {$listkeys}} ,hroom_cfg: {$hroom_cfg_keys} data success...<br>";
			$this->i("LoadVenue {lv: {$hkeys}, hv: {$listkeys} ,hroom_cfg: {$hroom_cfg_keys}} data success...");
		}	

        //公告缓存
		private function LoadAnnouncement() {
			$row = $this->mysql->find("SELECT id,title,content FROM `announcement` WHERE `status`=1 ORDER BY `create_time` DESC ");
			$this->clear(array("ha:*"));
			$total = 0;
			foreach ($row as $a) {
				$id = $a['id'];
				$this->cache_redis->hMset("ha:$id", $a);
				$total++;
			}
			$keys = count($this->cache_redis->keys("ha:*"));
			echo "LoadAnnouncement {ha: {$keys}} data success...<br>";
			$this->i("LoadAnnouncement {ha: {$keys}} data success...");
		}
		
		//vip信息缓存
		private function LoadVipConfig(){
			$row = $this->mysql->find("SELECT * FROM vip_config");
			$this->clear(array("hvipconfig:*"));
			$total = 0;
			foreach($row as $v){
				$id = $v['id'];
				$this->cache_redis->hMset("hvipconfig:$id", $v);
				$total++;
			}
			$keys = count($this->cache_redis->keys("hvipconfig:*"));
			echo "LoadVipConfig {hvipconfig: {$keys}} data success...<br>";
			$this->i("LoadVipConfig {hvipconfig: {$keys}} data success...");
		}
		
		//支付方式缓存
		private function LoadPayment(){
			$row = $this->mysql->find("SELECT * FROM payment");
			$this->clear(array("hp:*"));
			$total = 0;
			foreach($row as $v){
				$id = $v['id'];
				$this->cache_redis->hMset("hp:$id", $v);
				$total++;
			}
			$keys = count($this->cache_redis->keys("hp:*"));
			echo "LoadPayment {hp: {$keys}} data success...<br>";
			$this->i("LoadPayment {hp: {$keys}} data success...");
		}
		
		//商品信息缓存
		private function LoadGoods() {
			$row = $this->mysql->find("SELECT * FROM `goods` Where `status`=1");
		
			$this->clear(array("hg:*", "lg:ids"));
			foreach ($row as $g) {
				$id = $g['id'];
				$this->cache_redis->rPush("lg:ids", $id);
				$this->cache_redis->hMset("hg:{$id}", $g);	
			}
			
			$hkeys = count($this->cache_redis->keys("hg:*"));
			$listkeys = count($this->cache_redis->lRange("lg:ids", 0, -1));
			$this->cache_redis->hset("vga", "upgoods",TIME_STAMP);
			echo "LoadGoods {lg: {$hkeys}, hg: {$listkeys}} data success...<br>";
			$this->i("LoadGoods {lg: {$hkeys}, hg: {$listkeys}} data success...");
		}
		
		//奖励信息缓存
		private function LoadGetReward(){
		    $row = $this->mysql->find("SELECT * FROM `reward`");
            $this->clear(array("hreward:*"));
            foreach ($row as $g) {
				$id = $g['id'];
				$this->cache_redis->hMset("hreward:{$id}", $g);	
			}
            $keys = count($this->cache_redis->keys("hreward:*"));
			echo "LoadGetReward {hreward: {$keys}} data success...<br>";
			$this->i("LoadGetReward {hreward: {$keys}} data success...");			
		}
		
		private function LoadMarquee() {
			
			$row = $this->mysql->find("select * from `marquee` where `status` = 1 AND `type` = 1 ORDER BY `create_time` DESC LIMIT 0,5");
			$this->clear(array("hmarq:*","lmarq:ids"));
			foreach ($row as $mar) {
				$id = $mar['id'];
				$this->cache_redis->rPush("lmarq:ids", $id);
				$this->cache_redis->hMset("hmarq:{$id}",$mar);		
			}
			$hkeys = count($this->cache_redis->keys("hmarq:*"));
			$listkeys = count($this->cache_redis->lRange("lmarq:ids", 0, -1));						
			echo "LoadMarquee {lmarq: {$hkeys}, hmarq: {$listkeys}} data success...<br>";
			$this->i("LoadMarquee {lmarq: {$hkeys}, hmarq: {$listkeys}} data success...");
		}
		
		private function LoadGift() {
			$row = $this->mysql->find("select * from `gift`");
			$this->clear(array("hgift:*","lgift:ids"));
			foreach ($row as $gift) {
				$id = $gift['id'];
				$this->cache_redis->rPush("lgift:ids", $id);
				$this->cache_redis->hMset("hgift:{$id}",$gift);
			}
			$hkeys = count($this->cache_redis->keys("hgift:*"));
			$listkeys = count($this->cache_redis->lRange("lgift:ids", 0, -1));
			echo "LoadGift {lgift: {$hkeys}, hgift: {$listkeys}} data success...<br>";
			$this->i("LoadGift {lgift: {$hkeys}, hgift: {$listkeys}} data success...");
		}
		
		private function LoadAward() {
			$row = $this->mysql->find("select * from `award` where `status` = 1");
			$this->clear(array("haward:*","laward:ids"));
			foreach ($row as $award) {
				$id = $award['id'];
				$this->cache_redis->rPush("laward:ids", $id);
				$this->cache_redis->hMset("haward:{$id}",$award);
			}
			$hkeys = count($this->cache_redis->keys("haward:*"));
			$listkeys = count($this->cache_redis->lRange("laward:ids", 0, -1));
			echo "LoadAward {laward: {$hkeys}, haward: {$listkeys}} data success...<br>";
			$this->i("LoadAward {laward: {$hkeys}, haward: {$listkeys}} data success...");
		}
		
		private function LoadCoinTaskList() {
			$row = $this->mysql->find("select * from `coin_task_list` where `status` = 1");
			$this->clear(array("hcointask:*","lcointask:ids"));
			foreach ($row as $task) {
				$id = $task['id'];
				$this->cache_redis->rPush("lcointask:ids", $id);
				$this->cache_redis->hMset("hcointask:{$id}",$task);
			}
			$hkeys = count($this->cache_redis->keys("hcointask:*"));
			$listkeys = count($this->cache_redis->lRange("lcointask:ids", 0, -1));
			echo "Load {lCoinTask: {$hkeys}, hCoinTask: {$listkeys}} data success...<br>";
			$this->i("LoadCoinTask {lcointask: {$hkeys}, hcointask: {$listkeys}} data success...");
		}
		
		private function gameconfig(){
			$row = $this->mysql->find("select * from `config` where `id` = 1");
			$row = $row[0];
			$str = "<?php\n\r";
			
			$str .= "class Game {\n\r";

			$str .='public static $salt_password="'.$row['salt_password'].'";//passwor的salt'."\n\r";
			$str .='public static $appname="'.$row['appname'].'";//应用'."\n\r";
			$str .='public static $init_money = '.$row['init_money'].';// 初始金币'."\n\r"; 
			$str .='public static $init_coin = '.$row['init_coin'].';// 初始元宝'."\n\r";  
			$str .='public static $broke_money = '.$row['broke_money'].';// 破产金额条件'."\n\r"; 
			$str .='public static $broke_num_max = '.$row['broke_num_max'].';// 每日破产次数'."\n\r";
			$str .='public static $broke_give_money = '.$row['broke_give_money'].';// 破产每次赠送金币'."\n\r"; 	
			$str .='public static $login_reward_money = array('.$row['login_reward_money'].');// 连续登陆奖励金额'."\n\r"; 
			$str .='public static $can_reward_play_times = array('.$row['crpt'].');//玩多少局可以领取奖励'."\n\r"; 
			$str .='public static $play_times_reward_array = array('.$row['ptra'].');//玩局数对应奖励金额'."\n\r"; 	
			$str .='public static $can_reward_win_times = array('.$row['crwt'].');//胜利说少局可以领奖'."\n\r"; 
			$str .='public static $win_times_reward_array = array('.$row['wtra'].');//胜利说少局可以领奖金额'; 
			
			$str .="\n\r}\n\r";
			$str .="?>";

			/*if(!file_exists($src)){
				dirname($src);
				}*/
		   /* $dir = dirname(__FILE__);
			$src = '../../'.$dir."/config/game1.php";*/
			
           /* var_dump($src);
			exit;*/
			//file_put_contents($src,$str);
			echo "Load gameconfig data success...<br>";
			$this->i("LoadGameconfig  data success...");
		}
		
		private function LoadChangeCoinMoney() {
			$row = $this->mysql->find("select * from `change_coin_money` where 1 = 1");
			$this->clear(array("cm:*","cm:ids"));
			foreach ($row as $ccm) {
				$id = $ccm['id'];
				$this->cache_redis->rPush("cm:ids", $id);
				$this->cache_redis->hMset("cm:{$id}",$ccm);
			}
			$hkeys = count($this->cache_redis->keys("cm:*"));
			$listkeys = count($this->cache_redis->lRange("cm:ids", 0, -1));
			echo "LoadChangeCoinMoney {cm: {$hkeys}, cm: {$listkeys}} data success...<br>";
			$this->i("LoadChangeCoinMoney {cm: {$hkeys}, cm: {$listkeys}} data success...");
		}
		//LoadGetProps
		private function LoadGetProps() {
			$row = $this->mysql->find("select * from `propinfo` where 1 = 1");
			$this->clear(array("prop:*","prop:ids"));
			foreach ($row as $prop) {
				$id = $prop['id'];
				$this->cache_redis->rPush("prop:ids", $id);
				$this->cache_redis->hMset("prop:{$id}",$prop);
			}
			$hkeys = count($this->cache_redis->keys("prop:*"));
			$listkeys = count($this->cache_redis->lRange("prop:ids", 0, -1));
			echo "LoadGetProps {prop: {$hkeys}, prop: {$listkeys}} data success...<br>";
			$this->i("LoadGetProps {prop: {$hkeys}, prop: {$listkeys}} data success...");
		}
		
		private function LoadGetGames() {
			$row = $this->mysql->find("select * from `gameinfo` where 1 = 1");
			$this->clear(array("game:*","game:ids"));
			foreach ($row as $game) {
				$id = $game['NameID'];
				$this->cache_redis->rPush("game:ids", $id);
				$this->cache_redis->hMset("game:{$id}",$game);
			}
			$hkeys = count($this->cache_redis->keys("game:*"));
			$listkeys = count($this->cache_redis->lRange("game:ids", 0, -1));
			echo "LoadGetGames {game: {$hkeys}, game: {$listkeys}} data success...<br>";
			$this->i("LoadGetGames {game: {$hkeys}, game: {$listkeys}} data success...");
		}


		public function after() {
			$this->deinitCacheRedis();
			$this->deinitMysql();
		}
		
    }
?>
